﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Lottery.BLL;
using KZ.Exception;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class FrmValueAmount : Form
    {
        public FrmValueAmount()
        {
            InitializeComponent();
            dtgHeadValue1.AutoGenerateColumns = false;
            dtgHeadValue2.AutoGenerateColumns = false;
            dtgTailValue1.AutoGenerateColumns = false;
            dtgTailValue2.AutoGenerateColumns = false;

            colHeadAmount1.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colHeadAmount2.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailAmount1.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailAmount2.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colHeadWinAmount1.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colHeadWinAmount2.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailWinAmount1.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailWinAmount2.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colHeadRevenue1.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colHeadRevenue2.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailRevenue1.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailRevenue2.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
        }

        private void FrmValueAmount_Load(object sender, EventArgs e)
        {
            try
            {
                DateTime? from;
                DateTime? to;
                RuleDetailGroupReportBLL.GetFirstDateLastDate(out from, out to);

                if (!from.HasValue) { return; }

                DataTable dt = ValueBLL.GetValueList(to.Value.AddDays(1));
                string channelID = Utility.StringParse(dt.Rows[0]["ChannelID"]);
                string channelName = Utility.StringParse(dt.Rows[0]["ChannelName"]);

                tbpHead1.Text = "Đầu " + channelName;
                tbpTail1.Text = "Đuôi " + channelName;

                DataTable headDT = GetDataTable();

                DataRow[] dataRowList = dt.Select("ChannelID = '" + channelID + "'");
                foreach (DataRow row in dataRowList)
                {
                    headDT.ImportRow(row);
                    dt.Rows.Remove(row);
                }

                dtgHeadValue1.DataSource = headDT;
                dtgTailValue1.DataSource = headDT;

                txtHeadTotalAmount1.Text = Utility.CurrencyParse(Global.GetDtgTotal(dtgHeadValue1, colHeadAmount1.Index), Global.Culture);
                txtTailTotalAmount1.Text = Utility.CurrencyParse(Global.GetDtgTotal(dtgTailValue1, colTailAmount1.Index), Global.Culture);

                channelID = Utility.StringParse(dt.Rows[0]["ChannelID"]);
                channelName = Utility.StringParse(dt.Rows[0]["ChannelName"]);

                tbpHead2.Text = "Đầu " + channelName;
                tbpTail2.Text = "Đuôi " + channelName;

                DataTable tailDT = GetDataTable();

                dataRowList = dt.Select("ChannelID = '" + channelID + "'");
                foreach (DataRow row in dataRowList)
                {
                    tailDT.ImportRow(row);
                    dt.Rows.Remove(row);
                }

                dtgHeadValue2.DataSource = tailDT;
                dtgTailValue2.DataSource = tailDT;

                txtHeadTotalAmount2.Text = Utility.CurrencyParse(Global.GetDtgTotal(dtgHeadValue2, colHeadAmount1.Index), Global.Culture);
                txtTailTotalAmount2.Text = Utility.CurrencyParse(Global.GetDtgTotal(dtgTailValue2, colTailAmount1.Index), Global.Culture);

                decimal headTotalAmount1 = Utility.DecimalParse(txtHeadTotalAmount1.Text);
                decimal headTotalAmount2 = Utility.DecimalParse(txtHeadTotalAmount2.Text);
                decimal tailTotalAmount1 = Utility.DecimalParse(txtTailTotalAmount1.Text);
                decimal tailTotalAmount2 = Utility.DecimalParse(txtTailTotalAmount2.Text);
                decimal headPositive1 = 0;
                decimal headNegative1 = 0;
                decimal headPositive2 = 0;
                decimal headNegative2 = 0;
                decimal tailPositive1 = 0;
                decimal tailNegative1 = 0;
                decimal tailPositive2 = 0;
                decimal tailNegative2 = 0;
                decimal winAmount;
                for (int i = 0; i < 100; i++)
                {
                    winAmount = Utility.DecimalParse(dtgHeadValue1[colHeadWinAmount1.Index, i].Value);
                    dtgHeadValue1[colHeadRevenue1.Index, i].Value = winAmount - headTotalAmount1 * Global.CapitalRate / 100;

                    if (winAmount - headTotalAmount1 * Global.CapitalRate / 100 >= 0) { headPositive1++; }
                    else { headNegative1++; }

                    winAmount = Utility.DecimalParse(dtgHeadValue2[colHeadWinAmount2.Index, i].Value);
                    dtgHeadValue2[colHeadRevenue2.Index, i].Value = winAmount - headTotalAmount2 * Global.CapitalRate / 100;

                    if (winAmount - headTotalAmount2 * Global.CapitalRate / 100 >= 0) { headPositive2++; }
                    else { headNegative2++; }

                    winAmount = Utility.DecimalParse(dtgTailValue1[colTailWinAmount1.Index, i].Value);
                    dtgTailValue1[colTailRevenue1.Index, i].Value = winAmount - tailTotalAmount1 * Global.CapitalRate / 100;

                    if (winAmount - tailTotalAmount1 * Global.CapitalRate / 100 >= 0) { tailPositive1++; }
                    else { tailNegative1++; }

                    winAmount = Utility.DecimalParse(dtgTailValue2[colTailWinAmount2.Index, i].Value);
                    dtgTailValue2[colTailRevenue2.Index, i].Value = winAmount - tailTotalAmount2 * Global.CapitalRate / 100;

                    if (winAmount - tailTotalAmount2 * Global.CapitalRate / 100 >= 0) { tailPositive2++; }
                    else { tailNegative2++; }
                }

                lblHeadPositive1.Text = headPositive1.ToString();
                lblHeadNegative1.Text = headNegative1.ToString();
                lblHeadPositive2.Text = headPositive2.ToString();
                lblHeadNegative2.Text = headNegative2.ToString();
                lblTailPositive1.Text = tailPositive1.ToString();
                lblTailNegative1.Text = tailNegative1.ToString();
                lblTailPositive2.Text = tailPositive2.ToString();
                lblTailNegative2.Text = tailNegative2.ToString();
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("FrmValueAmount_Load", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private DataTable GetDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Value", typeof(String));
            dt.Columns.Add("HeadAmount", typeof(decimal));
            dt.Columns.Add("TailAmount", typeof(decimal));
            dt.Columns.Add("HeadWinAmount", typeof(decimal));
            dt.Columns.Add("TailWinAmount", typeof(decimal));
            dt.Columns.Add("HeadRevenue", typeof(decimal));
            dt.Columns.Add("TailRevenue", typeof(decimal));

            return dt;
        }
    }
}
